package com.augurit.gzcgwsp.state.util;

import java.awt.Color;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.net.MalformedURLException;   
  
import com.lowagie.text.Cell;   
import com.lowagie.text.Document;   
import com.lowagie.text.DocumentException;   
import com.lowagie.text.Element;   
import com.lowagie.text.Font;   
import com.lowagie.text.Image;   
import com.lowagie.text.PageSize;   
import com.lowagie.text.Paragraph;   
import com.lowagie.text.Phrase;   
import com.lowagie.text.Table;   
import com.lowagie.text.pdf.BaseFont;   
import com.lowagie.text.rtf.RtfWriter2;   
  
/**  
20. * 根据itext提供的java类库，构建word模板，并添加相应的内容，从而导出word报告；平台不相关  
21. * 需要引入iText-2.1.7.jar;iTextAsian.jar;iText-rtf-2.1.7.jar  
22. *   
23. * @author ryan  
24. */  
public class WordTemplete {   
   private Document document;   
    private BaseFont bfChinese;   
    public BaseFont getBfChinese() {   
        return bfChinese;   
    }   
 
    public void setBfChinese(BaseFont bfChinese) {   
        this.bfChinese = bfChinese;   
    }   
      public Document getDocument() {   
        return document;       }   
  
    public void setDocument(Document document) {   
       this.document = document;   
    }   
 
    public WordTemplete(){   
       this.document = new Document(PageSize.A4);   
           
    }   
   /**  
     * @param filePath 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中  
50.     * @throws DocumentException  
51.     * @throws IOException  
52.     */  
   public void openDocument(String filePath) throws DocumentException,   
    IOException {   
//       建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中   
        RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));   
       this.document.open();   
//       设置中文字体   
       this.bfChinese = BaseFont.createFont("STSongStd-Light",   
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
   }   
      
   /**  
64.     * @param titleStr 标题  
65.     * @param fontsize 字体大小  
66.     * @param fontStyle 字体样式  
67.     * @param elementAlign 对齐方式  
68.     * @throws DocumentException  
69.     */  
   public void insertTitle(String titleStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{   
       Font titleFont = new Font(this.bfChinese, fontsize, fontStyle);   
        Paragraph title = new Paragraph(titleStr);   
       // 设置标题格式对齐方式   
       title.setAlignment(elementAlign);   
       title.setFont(titleFont);   
           
        this.document.add(title);   
    }   
    /**  
81.     * @param contextStr 内容  
82.     * @param fontsize 字体大小  
83.     * @param fontStyle 字体样式  
84.     * @param elementAlign 对齐方式  
85.     * @throws DocumentException   
86.     */  
   public void insertContext(String contextStr,int fontsize,int fontStyle,int elementAlign) throws DocumentException{   
       // 正文字体风格   
       Font contextFont = new Font(bfChinese, fontsize, fontStyle);   
       Paragraph context = new Paragraph(contextStr);   
      //设置行距   
        // 正文格式左对齐   
       context.setAlignment(elementAlign);   
       context.setFont(contextFont);   
      // 离上一段落（标题）空的行数   
       context.setSpacingBefore(5);   
      // 设置第一行空的列数   
      context.setFirstLineIndent(20);   
       document.add(context);   
    }   

   /*  
     * 现状评估  
     *
     * */  
    public void insertRiskEvaluationTable() throws DocumentException{   
       Table aTable = new Table(7,30);   
       int width1[] = {10, 29, 12, 12, 16, 16, 16};   
       aTable.setWidths(width1);// 设置每列所占比例   
       aTable.setWidth(100); // 占页面宽度 90% 
       aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示   
       aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示   
       aTable.setAutoFillEmptyCells(true); // 自动填满   
       aTable.setBorderWidth(0); // 边框宽度   
       aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色   
           
      Font fontChinese = new Font(bfChinese, 10, Font.BOLD);         
      Image photo=null;
		try {
			photo = Image.getInstance("F:\\gzcgwsp\\gzcgwsp\\WebApp\\gzcgwsp\\state\\rqstate\\img\\tongjiword.jpg");
			photo.scaleAbsolute(180, 50);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		   Cell cell2 = new Cell(new Phrase("统计时段", fontChinese));   
	       cell2.setVerticalAlignment(Element.ALIGN_CENTER);   
	       cell2.setHorizontalAlignment(Element.ALIGN_CENTER);   
	       cell2.setRowspan(2); 
	       cell2.setColspan(7); 
	       cell2.setBorderColor(new Color(0, 0, 0));   
	       //cell3.setBackgroundColor(new Color(153, 204, 255));   
	       aTable.addCell(cell2);   
       Cell cell3 = new Cell(photo);   
       cell3.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell3.setHorizontalAlignment(Element.ALIGN_CENTER);   
       cell3.setRowspan(4); 
       cell3.setColspan(2); 
       cell3.setBorderColor(new Color(0, 0, 0));   
       //cell3.setBackgroundColor(new Color(153, 204, 255));   
       aTable.addCell(cell3);   
  
          
        Cell cell5 = new Cell(new Phrase("审批结果", fontChinese));   
        cell5.setVerticalAlignment(Element.ALIGN_CENTER);   
        cell5.setHorizontalAlignment(Element.ALIGN_CENTER);   
        cell5.setColspan(2);   
        cell5.setRowspan(2);  
        cell5.setBorderColor(new Color(0, 0, 0));   
        //cell5.setBackgroundColor(new Color(204, 255, 255));   
        aTable.addCell(cell5);   
          
       Cell cell6 = new Cell(new Phrase("完成时限", fontChinese));   
       cell6.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell6.setHorizontalAlignment(Element.ALIGN_CENTER);   
       cell6.setColspan(3);  
       cell6.setRowspan(2);  
       cell6.setBorderColor(new Color(0, 0, 0));   
       //cell6.setBackgroundColor(new Color(204, 255, 255));   
       aTable.addCell(cell6);   
    
       Cell cell7 = new Cell(new Phrase("同意", fontChinese));   
       cell7.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell7.setHorizontalAlignment(Element.ALIGN_CENTER);   
       Cell cell8 = new Cell(new Phrase("不同意", fontChinese));   
       cell8.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell8.setHorizontalAlignment(Element.ALIGN_CENTER);   
       cell8.setBorderColor(new Color(0, 0, 0));   
       cell7.setRowspan(2); 
       cell8.setRowspan(2); 
       //cell7.setBackgroundColor(new Color(204, 255, 255));   
       aTable.addCell(cell7);  
       aTable.addCell(cell8);   
       Cell cell9 = new Cell(new Phrase("按期完成(宗)", fontChinese));   
       cell9.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell9.setHorizontalAlignment(Element.ALIGN_CENTER);   
       Cell cell10 = new Cell(new Phrase("延期完成(宗)", fontChinese));   
       cell10.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell10.setHorizontalAlignment(Element.ALIGN_CENTER);   
       Cell cell11 = new Cell(new Phrase("超期完成(宗)", fontChinese));   
       cell11.setVerticalAlignment(Element.ALIGN_CENTER);   
       cell11.setHorizontalAlignment(Element.ALIGN_CENTER);  
       cell11.setBorderColor(new Color(0, 0, 0));   
       cell11.setRowspan(2); 
       cell10.setRowspan(2); 
       cell9.setRowspan(2); 
       aTable.addCell(cell9);   
       aTable.addCell(cell10);  
       aTable.addCell(cell11);  
       for(int i=0;i<20;i++){   
    	   Cell cells = new Cell(new Phrase("同意", fontChinese));   
           cells.setVerticalAlignment(Element.ALIGN_CENTER);   
           cells.setHorizontalAlignment(Element.ALIGN_CENTER);   
           cells.setRowspan(2); 
           aTable.addCell(cells);   
       }   
           
       document.add(aTable);   
       document.add(new Paragraph("\n"));   
    }   

       
   /**  
    * @param imgUrl 图片路径  
390.     * @param imageAlign 显示位置  
391.     * @param height 显示高度  
392.     * @param weight 显示宽度  
393.     * @param percent 显示比例  
394.     * @param heightPercent 显示高度比例  
395.     * @param weightPercent 显示宽度比例  
396.     * @param rotation 显示图片旋转角度  
397.     * @throws MalformedURLException  
398.     * @throws IOException  
399.     * @throws DocumentException  
400.     */  
    public void insertImg(String imgUrl,int imageAlign,int height,int weight,int percent,int heightPercent,int weightPercent,int rotation) throws MalformedURLException, IOException, DocumentException{   
//       添加图片   
       Image img = Image.getInstance(imgUrl);   
        if(img==null)   
           return;   
        img.setAbsolutePosition(0, 0);   
        img.setAlignment(imageAlign);   
       img.scaleAbsolute(height, weight);   
       img.scalePercent(percent);   
       img.scalePercent(heightPercent, weightPercent);   
        img.setRotation(rotation);   
 
        document.add(img);   
   }   
       
   public void closeDocument() throws DocumentException{   
        this.document.close();   
   }   
      
    public static void main(String[] args) throws DocumentException, IOException {   
        WordTemplete wt = new WordTemplete();   
        wt.openDocument("d:\\dome1.doc");   
        wt.insertTitle("城管委行政管理审批统计表", 12, Font.BOLD, Element.ALIGN_CENTER);   
        wt.insertRiskEvaluationTable();   
        wt.closeDocument();   
   }   
}  